package codeRandomThoughts.Test377组合总和IV;

/**
 * 尝试一下用二维数组
 * 错误方法
 */
public class Solution2 {
    public int combinationSum4(int[] nums, int target) {
        int[][] dp = new int[nums.length][target + 1];

        //初始化矩阵
        for (int i = 0; i < nums.length; i++) {
            dp[i][0] = 1;
        }

        for (int j = nums[0]; j <= target; j++) {
            dp[0][j] = 1;
        }

        for (int i = 1; i < nums.length; i++) {
            for (int j = nums[i]; j <= target; j++) {
                //第i个元素不放进去 和 第i个元素放进去
                dp[i][j] = dp[i - 1][j] + dp[i][j - nums[i]];
            }
        }

        System.out.println("先遍历物品,再遍历容量");
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j <= target; j++) {
                System.out.print(dp[i][j] + " ");
            }
            System.out.print("\n");
        }

        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += dp[i][target];
        }
        return sum;
    }
}
